package service

import (
	"fmt"
	"ideatools/application/constant"
	"ideatools/application/constant/enums"
	"ideatools/application/db/dbservice"
	"ideatools/application/dto"
	"ideatools/application/pkgs/clips"
	"ideatools/application/pkgs/filehelper"
	"ideatools/application/pkgs/mylog"
	"ideatools/application/pkgs/startup"
	"ideatools/application/pkgs/utils"
	"ideatools/application/pkgs/yaml"
	"ideatools/setting"
	"os/exec"
	"path/filepath"
	"runtime"
	"strconv"
)

type SystemService struct {
	ProjectName string
}

func NewSystemService(ProjectName_ string) *SystemService {
	return &SystemService{
		ProjectName: ProjectName_,
	}
}
func (s *SystemService) OpenFileBrowser(dir string) {
	var err error
	switch runtime.GOOS {
	case "windows":
		err = exec.Command("cmd", "/c", "start", dir).Run()
	case "darwin":
		err = exec.Command("open", dir).Run()
	default:
		err = fmt.Errorf("unsupported platform")
	}
	if err != nil {
		fmt.Printf("failed to open file explorer: %v\n", err)
	}
}

// GetSystemUserHomeDir 获取当前系统用户家目录
func (s *SystemService) GetSystemUserHomeDir() string {
	return utils.GetUserHomeDir()
}

// GetProjectName 获取当前项目名称
func (s *SystemService) GetProjectName() string {
	return s.ProjectName
}

// EntryDataDir 进入数据保存目录
func (s *SystemService) EntryDataDir() {
	s.OpenFileBrowser(filepath.Join(s.GetSystemUserHomeDir(), s.GetProjectName()))
}

func (s *SystemService) GetMousePos() dto.Pos {
	//x, y := robotgo.GetMousePos()
	//scale := GetScale()
	//mylog.Debug(fmt.Sprintf("缩放比例是%v", scale))
	return dto.Pos{
		PosX: 0,
		PosY: 0,
	}

}

func (s *SystemService) GetScreenSize() ScreenSize {

	//w, h := robotgo.GetScreenSize()
	//scale := GetScale()
	return ScreenSize{
		Width:  0,
		Height: 0,
	}

}

func (s *SystemService) StartListenKeyboard() {
	dbservice.Conf().SetKVT(constant.ConfigKeyFrontIsListenKeyboard, enums.BoolYes, enums.SettingTypeSystem)
}

func (s *SystemService) StoptListenKeyboard() {
	dbservice.Conf().SetKVT(constant.ConfigKeyFrontIsListenKeyboard, enums.BoolNo, enums.SettingTypeSystem)
}

func (s *SystemService) StartListenMouseLong() {
	dbservice.Conf().SetKVT(constant.ConfigKeyFrontIsListenMouseLongClick, enums.BoolYes, enums.SettingTypeSystem)
}

func (s *SystemService) StopListenMouseLong() {
	dbservice.Conf().SetKVT(constant.ConfigKeyFrontIsListenMouseLongClick, enums.BoolNo, enums.SettingTypeSystem)
}

func (s *SystemService) GetDBSize() string {
	dbpath := filepath.Join(utils.GetUserHomeDir(), setting.ProjectName, "data.db")
	mb, err := filehelper.GetFileSizeMB(dbpath)
	if err != nil {
		mylog.Error("GetDBSize() error" + err.Error())
		return ""
	}
	return mb
}

func (s *SystemService) GetCurrVersion() string {
	return setting.CurrVersion
}

func GetScale() float64 {
	conf := dbservice.NewDbConfigService()
	scaleStr := conf.Get(constant.ConfigKeyScreenScale)
	scale := 100.0
	if scaleStr != "" {
		scale, _ = strconv.ParseFloat(scaleStr, 64)
	}

	return scale / 100
}

func (s *SystemService) SetStartup(isStart bool) {
	err := startup.SetAutoStart(setting.ProjectName, isStart)
	if err != nil {
		mylog.Error("开机自启 error" + err.Error())
	}
}

func (s *SystemService) YamlSet(k string, v any) {
	yaml.Set(k, v)
}

func (s *SystemService) YamlGet(k string) any {
	return yaml.Get(k)
}

func (s *SystemService) ClipboardSet(text string) string {
	err := clips.SetText(text)
	if err != nil {
		return err.Error()
	}

	return ""
}

func (s *SystemService) ClipboardGet(text string) string {
	res, _ := clips.GetLeastText()
	return res
}

type ScreenSize struct {
	Width  int `json:"width"`
	Height int `json:"height"`
}
